package com.hzjh.h5mall.ui.wallet

import android.graphics.Paint
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.widget.dialog.JHEditDialog
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.weight.GlideUtils.loadCircleCorp
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.function.load
import com.hzjh.base.utils.other.CheckUtils
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.databinding.ActivityRechargeQrcodeBinding
import com.hzjh.h5mall.model.wallet.WalletVM

//充值二维码
@Route(path = RouteUrl.RECHARGE_QRCODE_ACTIVITY)
class RechargeQrcodeActivity : JHBaseActivity<ActivityRechargeQrcodeBinding,WalletVM>() {
    private var mBalance = ""
    override fun initData() {
        mBalance = intent.getStringExtra(RouteUrl.PARAMS_BALANCE)?:"0.00"
        viewBinding.tvBlance.text=CommUtils.getString(R.string.jh_balance_tip2,mBalance)
        viewModel.reqRechargeQrCode()
        viewModel.reqUserInfo()
    }

    override fun initView() {
        viewBinding.tvCopy.apply {
            paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
            paint.isAntiAlias = true;//抗锯齿
            setOnClickListener {
                //复制
                viewModel.payInfoBean.value?.wallet?.let {
                    CommUtils.copyText(it)
                    toastCenter(CommUtils.getString(R.string.jh_copy_succeed))
                }
            }
        }
        viewBinding.btnPayCode.setOnClickListener {
            //输入交易码
            JHEditDialog.Builder(this@RechargeQrcodeActivity)
                .setTile(CommUtils.getString(R.string.jh_payment_code))
                .setBtnTitle(CommUtils.getString(R.string.jh_submit))
                .setClickSubmit {
                    //提交交易码
                    viewModel.reqAddPayId(it)
                }
                .create().show()
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.userInfoBean.observe(this){
            viewBinding.ivHead.loadCircleCorp(it.avatar?:"")
            viewBinding.tvName.text=it.email
        }
        viewModel.payInfoBean.observe(this){
            viewBinding.ivQrcode.load(it.qrcode?:"")
            viewBinding.tvWalletAddress.text=it.wallet?:""
        }
        viewModel.addPayIdResult.observe(this){
            if(!CheckUtils.isEmpty(it)){
                //提交充值id成功
                navigationTo(RouteUrl.WALLET_DETAIL_ACTIVITY){
                    withString(RouteUrl.PARAMS_WALLET_ID,it)
                }
                setResult(RESULT_OK)
                finish()
            }
        }
    }

    override fun initViewBinding(): ActivityRechargeQrcodeBinding {
        return ActivityRechargeQrcodeBinding.inflate(layoutInflater)
    }
}